const db = uniCloud.database();
const _ = db.command;

exports.main = async (event) => {
  const { action, id } = event;
  
  console.log('event:', event);
  try {
    switch (action) {
      case 'viewswall':
        return await db.collection('wallpapers').doc(id).update({
          data: {
            views: db.command.inc(1),
            updateTime: Date.now()
          }
        });
      
      case 'downloadswall':
        return await db.collection('wallpapers').doc(id).update({
          data: {
            downloads: db.command.inc(1),
            updateTime: Date.now()
          }
        });
      
      case 'favoriteswall':
        return await db.collection('wallpapers').doc(id).update({
          data: {
            favorites: _.inc(1),
			views: db.command.inc(1),
            updateTime: Date.now()
          }
        });
      
      default:
        return { code: 404, msg: '无效操作' };
    }
  } catch (e) {
    console.error('Error in updateWallpaper:', e);
    return { code: 500, msg: e.message };
  }
}
